package com.gm.controller.card;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.gm.common.Pager;
import com.gm.controller.dto.CardDTO;
import com.gm.model.Base.Const;
import com.gm.model.po.Card;
import com.gm.model.po.CardType;
import com.gm.model.vo.ResMsg;
import com.gm.model.vo.ValidVo;
import com.gm.service.user.CardService;
import com.gm.service.user.CardTypeService;
import com.gm.tools.Common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户卡管理
 * Created by taojie on 17-6-21.
 */
@Controller
@RequestMapping("card")
public class CardController {

    Logger logger = LoggerFactory.getLogger(CardController.class);

    @Autowired
    private CardService cardService;
    @Autowired
    private CardTypeService cardTypeService;

    @RequestMapping("cardList.web")
    public ModelAndView cardList(Pager pager,Integer cid){
        ModelAndView view= new ModelAndView();
        PageList<CardDTO> cardList = cardService.getCardList(pager,cid);
        Paginator paginator =cardList.getPaginator();
        for(int i=0; i < cardList.size(); i++){
            cardList.get(i).setPwd("保密");
            cardList.get(i).setNum(Common.getBankAccount(cardList.get(i).getNum()));
        }
        view.addObject("cardList", cardList);
        view.addObject("cid", cid);
        view.addObject("paginator", paginator);
        view.setViewName("/card/card.jsp");
        return view;
    }

    /**
     * 跳转添加用户卡片
     * @param cid
     * @return
     */
    @RequestMapping("gotoAddCard.web")
    public ModelAndView gotoAddUser(Integer cid){
        ModelAndView view= new ModelAndView();
        List<CardType> cardTypeList = cardTypeService.getCardTypeAll();
        view.addObject("cid", cid);
        view.addObject("cardTypeList", cardTypeList);
        view.setViewName("/card/addCard.jsp");
        return view;
    }

    @RequestMapping(value = "checkCardNumOnly.do",method = RequestMethod.POST)
    @ResponseBody
    public ValidVo checkCardNumOnly(@RequestParam(value = "num", required = true) String num, RedirectAttributes attr){
        logger.info("Method[checkCardNumOnly],num[{}]",num);
        ValidVo validVo = new ValidVo();
        List cardList = cardService.selectByNum(num);
        if(cardList == null || cardList.size() == 0){
            validVo.setValid(true);
        }else{
            validVo.setValid(false);
        }
        logger.info("Method[checkCardNumOnly],validVo.isValid[{}]",validVo.isValid());
        return validVo;
    }

    @RequestMapping(value = "addCard.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg addUser(Card card, RedirectAttributes attr){
        logger.info("Method[Card],card.getNum[{}]",card.getNum());
        ResMsg resMsg = new ResMsg();
        int ret = cardService.addCard(card);
        if(ret == 1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("添加失败.");
        }
        logger.info("Method[Card],ret[{}]", ret);
        return resMsg;
    }

    @RequestMapping("gotoUpdateCard.web")
    public ModelAndView gotoUpdateCard(Integer cardId,Integer cid,Integer currentPage){
        ModelAndView view= new ModelAndView();
        List<CardType> cardTypeList = cardTypeService.getCardTypeAll();
        view.addObject("cid", cid);
        view.addObject("cardTypeList", cardTypeList);
        Card card = cardService.selectByPrimaryKey(cardId);
        view.addObject("currentPage", currentPage);
        view.addObject("card", card);
        view.setViewName("/card/updateCard.jsp");
        return view;
    }

    @RequestMapping(value = "updateCard.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg updateCard(Card card, Integer cidparam, Integer currentPage,RedirectAttributes attr){
        logger.info("Method[updateCard],card.getNum[{}]",card.getNum());
        ResMsg resMsg= new ResMsg();
        int ret = cardService.updateCard(card);
        if(ret == 1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("修改失败");
        }
        logger.info("Method[updateCard],ret[{}]",ret);

        return resMsg;
    }

    @RequestMapping("deleteCard.web")
    public ModelAndView deleteUser(Integer cardId,Integer cid, Integer currentPage, RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = cardService.deleteCard(cardId);
        if(ret==1){
            attr.addFlashAttribute("msg", "删除成功");
        }else{
            attr.addFlashAttribute("msg", "删除失败");
        }
        Pager pager = new Pager();
        pager.setCurrentPage(currentPage);
        List<CardDTO> list = cardService.getCardList(pager,cid);
        if(list.size() == 0){
            currentPage = currentPage -1;
        }
        if(currentPage==0)currentPage=1;
        view.addObject("currentPage", currentPage);
        view.setViewName("redirect:/card/cardList.web");
        return view;
    }


}
